﻿#region Includes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ArtheaEngine.Model;

#endregion

namespace ArtheaEditor
{
    public partial class EditorForm : Form
    {
        public EditorForm()
        {
            InitializeComponent();
        }

        private static Panel CreateFieldEditor<T>(object obj)
        {
            var layout = new TableLayoutPanel();

            foreach (var pi in typeof (T).GetProperties())
            {
                if (!pi.CanRead)
                    continue;

                var label = new Label {Text = pi.Name};

                layout.Controls.Add(label);

                if (pi.PropertyType.IsInstanceOfType(typeof (IEnumerable<T>)))
                {
                    //var ctl = new NestedCollectionEditor();

                    var ctl = new Label
                                  {Text = string.Format("{0} items", ((IQueryable<T>) pi.GetValue(obj, null)).Count())};

                    layout.Controls.Add(ctl);
                    continue;
                }

                var val = pi.GetValue(obj, null);
                var valctl = new TextBox {Text = val.ToString()};

                layout.Controls.Add(valctl);
            }
            return layout;
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new ImportDialog().ShowDialog();
        }

        private void roomsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ed = new CollectionEditor();

            ed.SetCollection(new List<Room>());

            ed.SetEditor(CreateFieldEditor<Room>(ed.SelectedItem));

            Controls.Clear();
            Controls.Add(ed);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
    }
}